﻿<%@ WebHandler Language="C#" Class="BargainStat" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class BargainStat : AjaxHandlerBase
{
    public BargainStat()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_BargainStat_Export);
    }
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //添加报备相关信息
                case "getlist":
                    GetList(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private void GetList(HttpContext context)
    {
        try
        {
            StringBuilder sb = new StringBuilder();
            //权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_BargainSearch_All))
            {
                sb.Append(" (1=1) ");
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_BargainSearch_City))
            {
                sb.AppendFormat(" EXISTS(Select 1 from v_Hr_DepartmentInfo M WHERE v_Contract_CommisionDetailInfo.DepartId=M.DepartId And M.cityid='{0}') ", CurrentUserInfo.CityId.ToString());
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_BargainSearch_Company))
            {
                sb.AppendFormat(" EXISTS(Select 1 from v_Hr_DepartmentInfo M WHERE v_Contract_CommisionDetailInfo.DepartId=M.DepartId And M.companyid='{0}') ", CurrentUserInfo.CompanyId.ToString());
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_BargainSearch_Depart))
            {
                sb.Append(" EXISTS(");
                sb.Append(string.Format("Select 1 from t_Business_EmpSetting M WHERE v_Contract_CommisionDetailInfo.DepartId=M.DepartId And M.Userid= '{0}' and SearchBargainStat=1", CurrentUserInfo.UserId.ToString()));
                sb.Append(") ");
            }
            else
            {
                sb.Append(" (1=0) ");
            }

            //查询4级
            Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
            Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
            Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
            Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);

            if (departs != Guid.Empty)
            {
                sb.AppendFormat(" and departid = '{0}'", departs);
            }
            else if (businessdistrict != Guid.Empty)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                    if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
                }

                if (item == null)
                {
                    context.Response.Write(retExecResult(Constants.ExecResult.Error, "获取商圈或分类数据丢失", ""));
                }
                sb.AppendFormat(" and (departid in (Select DepartId from v_Hr_DepartmentInfo where DepartSrc like '{0}%'))", item.DepartSrc);
            }
            else if (company != Guid.Empty)
            {
                sb.AppendFormat(" and (departid in (select DepartId from v_Hr_DepartmentInfo WHERE companyid='{0}')) ", company);
            }
            else if (city != Guid.Empty)
            {
                sb.AppendFormat(" and (departid in (Select DepartId from v_Hr_DepartmentInfo WHERE cityid='{0}')) ", city);
            }
            else
            {
                sb.AppendFormat(" and (departid in (select DepartId from v_Hr_DepartmentInfo WHERE companyid='{0}')) ", CurrentUserInfo.CompanyId);
            }

            if (!string.IsNullOrEmpty(context.Request["starttime"]))
            {
                sb.AppendFormat(" and ContractDate >= '{0}'", context.Request["starttime"]);
            }
            if (!string.IsNullOrEmpty(context.Request["endtime"]))
            {
                sb.AppendFormat(" and ContractDate <= '{0}'", context.Request["endtime"]);
            }

            sb.Append("  group by departid");
            
            string strSql = "select departid,dbo.FN_GetBussinessByDepartID(departid) as business,dbo.FN_GetDepartNameByDepartID(departid) as departname,"
                + "sum(case when BusinessType=10 then ApportionCommision else 0 end) as a_count,"
                + "sum(case when BusinessType=10 then point  else 0 end)/10 as a_sum,"
                + "sum(case when BusinessType=10 then point  else 0 end) as a_point,"
                + "sum(case when BusinessType=20 then ApportionCommision else 0 end) as b_count,"
                + "sum(case when BusinessType=20 then point  else 0 end)/10 as b_sum,"
                + "sum(case when BusinessType=20 then point  else 0 end) as b_point,"
                + "sum(case when BusinessType=10 then ApportionCommision  else 0 end)+"
                + "sum(case when BusinessType=20 then ApportionCommision  else 0 end) as totil,"
                + "sum(case when BusinessType=30 then ApportionCommision else 0 end) as c_count,"
                + "sum(case when BusinessType=30 then point  else 0 end)/10 as c_sum,"
                + "sum(case when BusinessType=30 then point  else 0 end) as c_point,"
                + "sum(case when BusinessType=40 then ApportionCommision else 0 end) as d_count,"
                + "sum(case when BusinessType=40 then point  else 0 end)/10 as d_sum,"
                + "sum(case when BusinessType=40 then point  else 0 end) as d_point,"
                + "sum(case when BusinessType=50 then ApportionCommision else 0 end) as e_count,"
                + "sum(case when BusinessType=50 then point  else 0 end)/10 as e_sum,"
                + "sum(case when BusinessType=50 then point  else 0 end) as e_point,"
                + "sum(case when BusinessType=10 then ApportionCommision  else 0 end)+"
                + "sum(case when BusinessType=20 then ApportionCommision  else 0 end)+"
                + "sum(case when BusinessType=30 then ApportionCommision  else 0 end)+"
                + "sum(case when BusinessType=40 then ApportionCommision  else 0 end)+sum(case when BusinessType=50 then ApportionCommision  else 0 end) as all_totil from v_Contract_CommisionDetailInfo where" + sb.ToString();
            
            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Business.DAL.v_Contract_CommisionDetailInfo.GetList(strSql);

            context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[0].Rows.Count, Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0])));
            
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }
}